define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap"
],function($,IO,bootbox,bootstrap){
    $(function () {
        getTable();
        function getTable(){
            var grid;
            //每列数据列性
            var columns = [
                {
                    title: '用户姓名',
                    dataIndex: 'user_name',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span>"+ data[datarow].user_name+"</span>";
                    }
                },
                {
                    title: '文章标题',
                    dataIndex: 'article_title',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span>"+ data[datarow].article_title+"</span>";
                    }
                },
                {
                    title: '评论时间',
                    dataIndex: 'gender',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        var date = new Date(data[datarow].timestamp);
                        create_year = date.getFullYear();
                        create_month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
                        create_day = date.getDate() < 10 ? '0'+date.getDate() : date.getDate() ;
                        create_hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() ;
                        create_min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() ;
                        create_sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                        return "<span>"+create_year+'-'+create_month+'-'+create_day+' '+create_hours+':'+create_min+':'+create_sec+ ' '+"</span>";
                    }
                },
                {
                    title: '评论内容',
                    dataIndex: 'gender',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        var back_data = data[datarow].comment_body;
                        var comment_body;
                        if(data[datarow].comment_body.length>10){
                            comment_body = back_data.substring(0,10) + "......";

                        }else{
                            comment_body = data[datarow].comment_body;
                        }
                        return "<span>"+comment_body +"</span>";
                    }
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span><a href='javascript:void(0)' class='delete' style='color: red;' article_comment_id='"+data[datarow].article_comment_id+"'>删除</a></span>&nbsp;&nbsp;&nbsp;<span><a href='javascript:void(0)' class='detail' style='color: green;' article_comment_id='"+data[datarow].article_comment_id+"'>详情</a></span>";
                    }
                },

            ];


            //数组初始化
            function dataInit() {
                grid = new Grid("/ct/act/comment_list", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

                //初始化
                grid.Init();
            }

            //获取输入框内容
            function get_input_param() {
                var user_name = $("#user_name").val();
                var article_title = $("#article_title").val();
                var comment_body = $("#comment_body").val();
                var params = "user_name=" + user_name + "&article_title=" + article_title + "&comment_body=" + comment_body;
                return params;
            }
            dataInit();

            //点击搜索查询
            $("#searchid").bind("click", function () {
                dataInit();
            });

        }



        /**---------------------------------获取展示详情--------------------------------*/
        $(document).on("click",".detail",function(){
            var article_comment_id=$(this).attr("article_comment_id");
            IO.get("/ct/act/comment_detail",{article_comment_id:article_comment_id},function(d){
                $("#user_name1").val(d.data.user_name);
                $("#article_title1").val(d.data.article_title);
                $("#comment_body1").text(d.data.comment_body);

                var date = new Date(d.data.timestamp);
                create_year = date.getFullYear() + '-';
                create_month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
                create_day = date.getDate() + ' ';
                create_hours = date.getHours() + ':';
                create_min = date.getMinutes() + ':';
                create_sec = date.getSeconds() + ' ';
                $("#timestamp1").val(create_year+create_month+create_day+create_hours+create_min+create_sec);
            });
            $("#myModal").modal('show');

        });
        /**---------------------------------删除--------------------------------*/
        $(document).on("click",".delete",function(){
            var article_comment_id=$(this).attr("article_comment_id");
            bootbox.confirm("确定要删除该评论？",function(result) {
                if(result) {
                    IO.get("/ct/act/comment_delete", {article_comment_id: article_comment_id}, function (d) {
                        if (d.code == 0) {
                            bootbox.alert(d.msg, function () {
                                getTable();
                            })
                        }

                    })
                }
            })
        })

    });

})